use async_trait::async_trait;
use clap::Parser;

use crate::commands::commands::Command;
use crate::models::models;
use crate::models::models::command::CommandState;
use crate::utils::command;

#[derive(Debug, Default, Clone, Parser)]
#[clap(disable_help_flag = true)]
struct Args {
    // 是否转大写
    #[arg(short = 'u', long = "upper", default_value_t = false)]
    upper: bool,
    // 字符串
    value: String,
}

#[derive(Debug, Default, Clone)]
pub(crate) struct Md5Command {}

#[async_trait]
impl Command for Md5Command {
    async fn execute(&self, command: models::command::Command) -> models::command::Command {
        let args = command::parse_command_args::<Args>(command.args.as_str());
        if args.is_err() {
            return self.save_command_output(command, CommandState::Success, args.unwrap_err().to_string());
        }

        let args = args.unwrap();

        let mut value = format!("{:x}", md5::compute(args.value.as_str()));
        if args.upper {
            value = value.to_uppercase();
        }

        self.save_command_output(command, CommandState::Success, value)
    }
}